<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>

  手写new


  <script>
    // 1.原生new特点
    function Person(name) {
      this.name = name
      // return 123 // 返回不是引用值，没影响
      // return {} // 返回引用值 new会打印引用
    }

    Person.prototype.say = function() {
      console.log('hello');
    }
    // console.log(new Person('zhangsan'))

    // 2.手撕new
    Function.prototype.new = function() {
      let fn = this // ->Person
      let obj = Object.create(fn.prototype) // 赋予new实例prototy
      let res = fn.apply(obj, arguments)
      return res instanceof Object ? res : obj
    }


    let p = Person.new('zhangsan')
    console.log(p);
    p.say()





    


    


  </script>
</body>
</html>